package com.zlzxm.lock;

import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

import static android.view.WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;

/**
 * Created by zlz
 * on  2019-10-14
 */
public class LockActivity extends BaseActivity {

    private String week[] = new String[]{"周日","周一","周二","周三","周四","周五","周六"};


    private TextView mTvTimer;
    private TextView mTvCurrntTimer;
    private TextView mTvCurrntWeek;
    private TextView mTvNoteTip;

    Timer timer = new Timer();

    long targetTimer = 0;

    MyTimerTask myTimerTask;

    SimpleDateFormat simpleDateFormat;

    SimpleDateFormat currentTime ;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_lock);
        getWindow().addFlags(FLAG_SHOW_WHEN_LOCKED);
        getWindow().addFlags(FLAG_DISMISS_KEYGUARD);

        setTransparent();

        mTvTimer = findViewById(R.id.tvTime);
        mTvCurrntTimer = findViewById(R.id.tvCurrentTime);
        mTvCurrntWeek = findViewById(R.id.tvWeek);
        mTvNoteTip = findViewById(R.id.tvNote);

        simpleDateFormat  = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        currentTime  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());

        String strTime = "2019-01-18";

        myTimerTask =  new MyTimerTask();

        try {
            Date date = simpleDateFormat.parse(strTime);

            targetTimer = date.getTime();

        } catch (ParseException e) {
            e.printStackTrace();
        }

        timer.schedule(myTimerTask,0,1000);



        String noteStr = "2019-10-23";
        String result = null;
        try {

            Date dateNote = simpleDateFormat.parse(noteStr);

             result = convertTime(dateNote.getTime() - System.currentTimeMillis());

             if(result.contains("天")){

                 int index = result.indexOf("天");

                 result = result.substring(0,index+1);

             }

        } catch (ParseException e) {
            e.printStackTrace();
        }



        mTvNoteTip.setText("提醒体检报告还剩: "+result);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(myTimerTask != null){

            myTimerTask.cancel();

        }

        if(timer!=null){

            timer.cancel();
            timer = null;
        }

    }

    private class MyTimerTask extends TimerTask{

        @Override
        public void run() {

            final   Date date = new Date();

            final long diff = date.getTime() - targetTimer;

            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    mTvCurrntTimer.setText(currentTime.format(date));

                    Calendar calendar = Calendar.getInstance();

                    mTvCurrntWeek.setText(week[calendar.get(Calendar.DAY_OF_WEEK)-1]);

                }
            });

            if(mTvTimer!= null){

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mTvTimer.setText(convertTime(diff));
                    }
                });


            }

        }
    }

    private String convertTime(long diff){

        long left = 0;
        //计算几天
        long day = diff / (1000*60*60*24);
        left = diff % (1000*60*60*24);

        //计算小时
        long hour = left /(1000*60*60);

        left = left % (1000*60*60);

        //计算几分钟：

        long m =left/(1000*60);

        left = left%(1000*60);

        //计算几秒
        long s = left/1000;


        return day+"天"+hour + "小时" + m +"分" + s +"秒";

    }

}
